using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace AuthService.Services;

/// <summary>
/// 认证授权服务接口
/// </summary>
public interface IAuthService : IApplicationService
{
    /// <summary>
    /// 用户登录
    /// </summary>
    Task<LoginResultDto> LoginAsync(LoginRequestDto input);

    /// <summary>
    /// 用户登出
    /// </summary>
    Task LogoutAsync(LogoutRequestDto input);

    /// <summary>
    /// 刷新Token
    /// </summary>
    Task<RefreshTokenResultDto> RefreshTokenAsync(RefreshTokenRequestDto input);

    /// <summary>
    /// 验证Token
    /// </summary>
    Task<TokenValidationResultDto> ValidateTokenAsync(string token);

    /// <summary>
    /// 获取Token信息
    /// </summary>
    Task<TokenInfoDto> GetTokenInfoAsync(string token);

    /// <summary>
    /// 获取用户上下文
    /// </summary>
    Task<UserContextDto> GetUserContextAsync(string token);

    /// <summary>
    /// 检查用户权限
    /// </summary>
    Task<bool> HasPermissionAsync(string userId, string permission);

    /// <summary>
    /// 获取用户权限列表
    /// </summary>
    Task<List<string>> GetUserPermissionsAsync(string userId);

    /// <summary>
    /// 获取用户角色列表
    /// </summary>
    Task<List<string>> GetUserRolesAsync(string userId);

    /// <summary>
    /// 密码重置
    /// </summary>
    Task ResetPasswordAsync(ResetPasswordRequestDto input);

    /// <summary>
    /// 修改密码
    /// </summary>
    Task ChangePasswordAsync(ChangePasswordRequestDto input);
}

/// <summary>
/// 登录请求DTO
/// </summary>
public class LoginRequestDto
{
    public string Username { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;
    public string ClientId { get; set; } = string.Empty;
    public string ClientSecret { get; set; } = string.Empty;
    public string Realm { get; set; } = "master";
    public string? Scope { get; set; }
    public bool RememberMe { get; set; } = false;
}

/// <summary>
/// 登录结果DTO
/// </summary>
public class LoginResultDto
{
    public string AccessToken { get; set; } = string.Empty;
    public string RefreshToken { get; set; } = string.Empty;
    public string TokenType { get; set; } = string.Empty;
    public int ExpiresIn { get; set; }
    public int RefreshExpiresIn { get; set; }
    public UserContextDto UserContext { get; set; } = new();
}

/// <summary>
/// 登出请求DTO
/// </summary>
public class LogoutRequestDto
{
    public string AccessToken { get; set; } = string.Empty;
    public string RefreshToken { get; set; } = string.Empty;
}

/// <summary>
/// 刷新Token请求DTO
/// </summary>
public class RefreshTokenRequestDto
{
    public string RefreshToken { get; set; } = string.Empty;
    public string ClientId { get; set; } = string.Empty;
    public string ClientSecret { get; set; } = string.Empty;
    public string Realm { get; set; } = "master";
}

/// <summary>
/// 刷新Token结果DTO
/// </summary>
public class RefreshTokenResultDto
{
    public string AccessToken { get; set; } = string.Empty;
    public string RefreshToken { get; set; } = string.Empty;
    public string TokenType { get; set; } = string.Empty;
    public int ExpiresIn { get; set; }
    public int RefreshExpiresIn { get; set; }
}

/// <summary>
/// Token验证结果DTO
/// </summary>
public class TokenValidationResultDto
{
    public bool IsValid { get; set; }
    public string? ErrorMessage { get; set; }
    public TokenInfoDto? TokenInfo { get; set; }
}

/// <summary>
/// Token信息DTO
/// </summary>
public class TokenInfoDto
{
    public bool Active { get; set; }
    public string Subject { get; set; } = string.Empty;
    public string Username { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public List<string> Roles { get; set; } = new();
    public Dictionary<string, object> RealmAccess { get; set; } = new();
    public Dictionary<string, object> ResourceAccess { get; set; } = new();
    public DateTime ExpirationTime { get; set; }
    public string IssuedAt { get; set; } = string.Empty;
    public string Issuer { get; set; } = string.Empty;
    public string Realm { get; set; } = "master";
}

/// <summary>
/// 用户上下文DTO
/// </summary>
public class UserContextDto
{
    public string UserId { get; set; } = string.Empty;
    public string Username { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public List<string> Roles { get; set; } = new();
    public List<string> Permissions { get; set; } = new();
    public string Realm { get; set; } = "master";
    public Guid? TenantId { get; set; }
    public DateTime LoginTime { get; set; }
    public DateTime LastActivityTime { get; set; }
}

/// <summary>
/// 重置密码请求DTO
/// </summary>
public class ResetPasswordRequestDto
{
    public string Email { get; set; } = string.Empty;
    public string Realm { get; set; } = "master";
}

/// <summary>
/// 修改密码请求DTO
/// </summary>
public class ChangePasswordRequestDto
{
    public string UserId { get; set; } = string.Empty;
    public string CurrentPassword { get; set; } = string.Empty;
    public string NewPassword { get; set; } = string.Empty;
    public string ConfirmPassword { get; set; } = string.Empty;
    public string Realm { get; set; } = "master";
}